package cn.iocoder.yudao.module.vlog.statistics.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.business.basic.pojo.bo.DataComparison;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortablePageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.vlog.statistics.dal.dataobject.VLogStatisticsDO;
import cn.iocoder.yudao.module.vlog.statistics.service.VLogStatisticsService;
import cn.iocoder.yudao.module.vlog.statistics.vo.VLogStatisticsReqVO;
import cn.iocoder.yudao.module.vlog.statistics.vo.VLogStatisticsRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "管理后台 - 视频统计")
@RestController
@RequestMapping("/vlog/statistics")
@Validated
public class VLogStatisticsController {

    @Resource
    private VLogStatisticsService vlogStatisticsService;

    @Resource
    private MemberUserApi memberUserApi;

    @GetMapping("/analyse")
    @Operation(summary = "获得视频统计分析")
    //@PreAuthorize("@ss.hasPermission('statistics:vlog:query')")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<DataComparison<VLogStatisticsRespVO>> getVLogStatisticsAnalyse(VLogStatisticsReqVO reqVO) {
        return success(vlogStatisticsService.getVLogStatisticsAnalyse(reqVO));
    }

    @GetMapping("/list")
    @Operation(summary = "获得视频统计明细（日期维度）")
    //@PreAuthorize("@ss.hasPermission('statistics:vlog:query')")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<List<VLogStatisticsRespVO>> getVLogStatisticsList(VLogStatisticsReqVO reqVO) {
        List<VLogStatisticsDO> list = vlogStatisticsService.getVLogStatisticsList(reqVO);
        return success(BeanUtils.toBean(list, VLogStatisticsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出获得视频统计明细 Excel（日期维度）")
    //@PreAuthorize("@ss.hasPermission('statistics:vlog:export')")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVLogStatisticsExcel(VLogStatisticsReqVO reqVO, HttpServletResponse response) throws IOException {
        List<VLogStatisticsDO> list = vlogStatisticsService.getVLogStatisticsList(reqVO);
        // 导出 Excel
        List<VLogStatisticsRespVO> voList = BeanUtils.toBean(list, VLogStatisticsRespVO.class);
        ExcelUtils.write(response, "视频状况.xls", "数据", VLogStatisticsRespVO.class, voList);
    }

    @GetMapping("/rank-page")
    @Operation(summary = "获得视频统计排行榜分页（视频维度）")
    //@PreAuthorize("@ss.hasPermission('statistics:vlog:query')")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<PageResult<VLogStatisticsRespVO>> getVLogStatisticsRankPage(@Valid VLogStatisticsReqVO reqVO,
                                                                                    @Valid SortablePageParam pageParam) {
        PageResult<VLogStatisticsDO> pageResult = vlogStatisticsService.getVLogStatisticsRankPage(reqVO, pageParam);
        // 处理视频信息
        Set<Long> userIds = convertSet(pageResult.getList(), VLogStatisticsDO::getUserId);
        List<UserSimpleVO> users = memberUserApi.getUserSimpleList(userIds);
        return success(BeanUtils.toBean(pageResult, VLogStatisticsRespVO.class,
                vo -> {
                    UserSimpleVO user = users.stream().filter(u -> Objects.equals(u.getId(), vo.getUserId())).findFirst().orElse(null);
                    vo.setUser(user);
                }));
    }

}
